package cn.com.dhc.danlu.widget.couponview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.LinearLayout;

import cn.com.dhc.danlu.R;

/**
 * @author: 李一方
 * @time: 2016-09-18 16:12
 * @mail: liyifang@danlu.com
 * @desc: 红包视图
 */
public class RedBegView extends LinearLayout {
    private final static boolean IS_AVAILABLE = true;
    private final static int BASE_COLOR = Color.parseColor ("#F26643");
    private final static int SHADOW_COLOR = Color.parseColor ("#33000000");
    private final static int INNER_GAP = 5;
    boolean isAvailable;
    private Paint mPaint;
    private Path path;
    private int innerGap;
    private int shadowColor;
    private int bottomColor;
    private int innerRectColor;
    private int ovalColor;
    private int baseColor;

    public RedBegView (Context context) {
        this (context, null);
    }

    public RedBegView (Context context, AttributeSet attrs) {
        super (context, attrs);
        init (attrs);
    }

    private void init (AttributeSet attrs) {
        initPaint ();
        initAttrs (attrs);
        setLayerType (LAYER_TYPE_SOFTWARE, null);
    }

    private void initPaint () {
        mPaint = new Paint (Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle (Paint.Style.FILL);
        path = new Path ();
    }

    private void initAttrs (AttributeSet attrs) {
        TypedArray ta = getContext ().obtainStyledAttributes (attrs, R.styleable.RedBegView);
        baseColor = ta.getColor (R.styleable.RedBegView_baseColor, BASE_COLOR);
        innerGap = ta.getDimensionPixelSize (R.styleable.RedBegView_innerGap, (int) TypedValue.applyDimension (TypedValue.COMPLEX_UNIT_DIP, INNER_GAP, getContext ().getResources ().getDisplayMetrics ()));
        isAvailable = ta.getBoolean (R.styleable.RedBegView_isAvailiable, IS_AVAILABLE);
        shadowColor = ta.getColor (R.styleable.RedBegView_shadowColor, SHADOW_COLOR);
        ta.recycle ();
    }

    public void setAvailable (boolean available) {
        final int color = baseColor;
        if (available) {
            setBackgroundColor (color);
            bottomColor = getSimilarColor (color, 0f, 0.05f, -0.05f);
            innerRectColor = getSimilarColor (color, 0f, -0.05f, 0.05f);
            ovalColor = getSimilarColor (color, 0f, 0.05f, 0f);
        } else {
            int temColor = getSimilarColor (color, -1f, -1f, -.2f);
            setBackgroundColor (temColor);
            bottomColor = getSimilarColor (temColor, 0f, 0f, -.07f);
            innerRectColor = getSimilarColor (temColor, 0f, 0f, .05f);
            ovalColor = getSimilarColor (temColor, 0f, 0f, 0f);
        }
    }

    /**
     * 通过HSV获得一个近似颜色
     *
     * @param baseColor
     *         基础颜色
     * @param hue
     *         色调
     * @param saturation
     *         饱和度
     * @param value
     *         明度
     *
     * @return 基础色的近似色
     */
    public int getSimilarColor (int baseColor, float hue, float saturation, float value) {
        float[] hsv = new float[3];
        Color.colorToHSV (baseColor, hsv);
        hsv[0] = hsv[0] + hue; // 色调
        hsv[1] = hsv[1] + saturation; // 饱和度
        hsv[2] = hsv[2] + value; // 明度
        return Color.HSVToColor (hsv);
    }

    @Override
    protected Parcelable onSaveInstanceState () {
        Bundle bundle = new Bundle ();
        Parcelable superData = super.onSaveInstanceState ();
        bundle.putParcelable ("superData", superData);
        bundle.putBoolean ("isAvailable", isAvailable);
        bundle.putInt ("baseColor", baseColor);
        return bundle;
    }

    @Override
    protected void onRestoreInstanceState (Parcelable state) {
        Bundle bundle = (Bundle) state;
        Parcelable superData = bundle.getParcelable ("super_data");
        baseColor = bundle.getInt ("baseColor");
        isAvailable = bundle.getBoolean ("isAvailable");
        super.onRestoreInstanceState (superData);
    }

    @Override
    protected void onDraw (Canvas canvas) {
        super.onDraw (canvas);

        // 绘制内长方形
        mPaint.setColor (innerRectColor);
        mPaint.setShadowLayer (0, 0, 0, 0);
        canvas.drawRect (innerGap, innerGap, getWidth () - innerGap, getHeight () - innerGap, mPaint);
        canvas.save ();

        // 绘制梯形
        mPaint.setColor (bottomColor);
        mPaint.setShadowLayer (0, 0, 0, 0);
        path.moveTo (0, getHeight ());
        path.lineTo (innerGap, getHeight () - innerGap);
        path.lineTo (getWidth () - innerGap, getHeight () - innerGap);
        path.lineTo (getWidth (), getHeight ());
        path.close ();
        canvas.drawPath (path, mPaint);
        canvas.save ();

        // 绘制圆弧
        mPaint.setColor (ovalColor);
        mPaint.setShadowLayer (10, 5, 5, shadowColor);
        float flag = 1.414f;
        canvas.drawCircle (getWidth () / 2, getHeight () / 2 - getWidth () / flag, getWidth () / flag, mPaint);
        canvas.save ();
    }
}